package com.huike.web.controller.review;


import com.github.pagehelper.Page;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.domain.Review;
import com.huike.review.service.ReviewService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * =========================================================新增数据============================================
     */
    /*
     * 新增接口1：
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name, @PathVariable Integer age, @PathVariable String sex) {
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        review.setCreateBy("1");
        review.setUpdateBy("1");
        int count = reviewService.insertReview(review);
        return count > 0 ? AjaxResult.success(200, "成功插入:1条数据", null) : AjaxResult.error("添加失败");
    }

    /*
     * 新增接口2：
     */
    @GetMapping("/saveData")
    public AjaxResult save(@RequestBody Review review) {
        int count = reviewService.insertReview(review);
        return count > 0 ? AjaxResult.success(200, "成功插入:1条数据", null) : AjaxResult.error("添加失败");
    }

    /**
     * =========================================================删除数据=============================================
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        int count = reviewService.deleteReview(id);
        return count > 0 ? AjaxResult.success(200, "成功删除:1条数据", null) : AjaxResult.error("删除失败");
    }

    /**
     * =========================================================修改数据=============================================
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review) {
        int count = reviewService.update(review);
        return count > 0 ? AjaxResult.success(200, "成功修改:1条数据", null) : AjaxResult.error("修改失败");
    }

    /**
     * =========================================================查询数据=============================================
     */
    @GetMapping("/getById")
    public AjaxResult selectReviewById(Integer id) {
        Review review = reviewService.selectReviewById(id);
        return review != null ? AjaxResult.success(200, "操作成功", review) : AjaxResult.error();
    }

    @GetMapping("/getDataByPage")
    public AjaxResult getDataByPage(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        List<Review> reviews = reviewService.DataByPage(pageNum, pageSize);
        int total = reviewService.countTotal();
        Map<Object, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",reviews);
        return AjaxResult.success(200,"查询成功",map);
    }
}